<?php
/**
 * Created by PhpStorm.
 * User: 86151 - 姜伟
 * Date: 2021/6/10
 * Time: 20:46.
 */

namespace Xinyu\Gen\Commands;

use Xinyu\Gen\Core\Config\CommonConfig;
use Xinyu\Gen\Core\GenClient;
use Xinyu\Gen\Core\Libs\CommonClient;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class CreateAllCommand extends Command
{
    /**
     * --module 【模块层，取值admin、web】
     * --name 【模型名】
     * --unique 【唯一字段，多个以英文逗号分隔，如name,age】
     * --search 【搜索参数】
     * --except 【过滤字段，多个以英文逗号分隔，如status,zz_at】.
     */
    protected $signature = 'a {--module=} {--name=} {--unique=} {--search=} {--except=}';

    protected $description = '生成controller/service/model/validator脚本';

    public function handle()
    {
        if ('local' !== env('APP_ENV')) {
            $this->error('该环境不允许执行该脚本');
            return false;
        }
        $this->createAll();
    }

    // 生成controller/service/model/validator脚本
    public function createAll()
    {
        $name = Str::studly(CommonClient::getInstance()->getName($this->option('name')));
        $module = Str::studly($this->option('module') ?? CommonConfig::MODULE_ADMIN);
//        $nameParams = CommonClient::getInstance()->getNameParamsByName($name);
//        $savePath = $nameParams['save_path'] ?? '';
//        if ($this->confirm("您确定要生成{$module}{$savePath}模块对应的所有CURD文件吗?")) {}
        $resList = GenClient::getInstance()->createAll([
            'module' => $module,
            'name' => $name,
            'unique' => $this->option('unique'),
            'search' => $this->option('search'),
            'except' => $this->option('except'),
        ]);
        if (isset($resList['code'])) {
            -1 === $resList['code'] ? $this->error($resList['msg']) : $this->info($resList['msg']);
            return false;
        }
        foreach ($resList as $res) {
            -1 === $res['code'] ? $this->error($res['msg']) : $this->info($res['msg']);
        }
    }
}
